package com.hyd.manage.service.impl;

import java.util.List;
import com.hyd.common.utils.DateUtils;
import com.hyd.manage.domain.Medicines;
import com.hyd.manage.domain.SalesRecord;
import com.hyd.manage.mapper.MedicinesMapper;
import com.hyd.manage.mapper.SalesRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hyd.manage.mapper.SalesMapper;
import com.hyd.manage.domain.Sales;
import com.hyd.manage.service.ISalesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * post销售Service业务层处理
 * 
 * @author hwb
 * @date 2025-02-19
 */
@Service
public class SalesServiceImpl implements ISalesService 
{
    @Autowired
    private SalesMapper salesMapper;
    @Autowired
    private SalesRecordMapper salesRecordMapper;
    @Autowired
    private MedicinesMapper medicinesMapper;

    /**
     * 查询post销售
     * 
     * @param medicineCode post销售主键
     * @return post销售
     */
    @Override
    public Sales selectSalesById(String medicineCode)
    {
        return salesMapper.selectSalesByBatchNumber(medicineCode);
    }

    /**
     * 查询post销售列表
     * 
     * @param sales post销售
     * @return post销售
     */
    @Override
    public List<Sales> selectSalesList(Sales sales)
    {
        return salesMapper.selectSalesList(sales);
    }

    /**
     * 新增post销售
     * 
     * @param sales post销售
     * @return 结果
     */
    @Override
    public int insertSales(Sales sales)
    {
        sales.setCreateTime(DateUtils.getNowDate());
        return salesMapper.insertSales(sales);
    }

    /**
     * 修改post销售
     * 
     * @param sales post销售
     * @return 结果
     */
    @Override
    public int updateSales(Sales sales)
    {
        sales.setUpdateTime(DateUtils.getNowDate());
        return salesMapper.updateSales(sales);
    }

    /**
     * 批量删除post销售
     * 
     * @param ids 需要删除的post销售主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSalesByIds(Long[] ids)
    {
        for (Long id : ids) {
            // 根据id查询post销售
            SalesRecord sales = salesMapper.selectSalesById(id);
            // 增加对应库存数量
            medicinesMapper.updateStockQuantityAdd(sales.getMedicineCode(), sales.getBatchNumber(), sales.getQuantity());
        }
        return salesMapper.deleteSalesByIds(ids);
    }

    /**
     * 删除post销售信息
     * 
     * @param id post销售主键
     * @return 结果
     */
    @Override
    public int deleteSalesById(Long id)
    {
        return salesMapper.deleteSalesById(id);
    }

    /**
     * 收款
     * @param salesRecord
     * @return
     */
    @Override
    public int receipt(SalesRecord salesRecord) {

        //salesRecordMapper.insertSalesRecord(salesRecord);
        return salesMapper.deleteAll();
    }

    /**
     * 挂单
     * @param ids
     * @return
     */
    @Override
    public int hangSales(Long[] ids) {
        return salesMapper.hangSalesByIds(ids);
    }
}
